/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.dao;

import east2west.dto.LocationDTO;
import east2west.dto.SchedulesDTO;
import east2west.dto.SchedulesLocationDTO;
import east2west.interfaces.LocationInterface;
import east2west.util.DatabaseUtil;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author thanhluan
 */
public class LocationDAO implements LocationInterface {

    Connection con = null;
    CallableStatement call = null;
    LocationDTO dto = null;
    ResultSet rs = null;

    public ArrayList<LocationDTO> readByAll() {
        ArrayList<LocationDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("select * from V_Localtion");
            rs = call.executeQuery();
            list = new ArrayList<LocationDTO>();
            while (rs.next()) {
                dto = new LocationDTO();
                dto.setLocationId(rs.getInt(1));
                dto.setLocationName(rs.getString(2));
                dto.setDescription(rs.getString(3));
                list.add(dto);
            }
            rs.close();
            call.close();
            return list;
        } catch (SQLException ex) {

            return null;
        }

    }

    public ArrayList<LocationDTO> readSByAll() {
        ArrayList<LocationDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("select  distinct LocationName from V_Location");
            rs = call.executeQuery();
            list = new ArrayList<LocationDTO>();
            while (rs.next()) {
                dto = new LocationDTO();

                dto.setLocationName(rs.getString(1));

                list.add(dto);
            }

            return list;
        } catch (SQLException ex) {

            return null;
        }

    }

    public LocationDTO findById(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("select * from V_Localtion where LocationId = ?");
            call.setInt(1, id);
            rs = call.executeQuery();
            while (rs.next()) {
                dto = new LocationDTO();
                dto.setLocationId(rs.getInt(1));
                dto.setLocationName(rs.getString(2));
                dto.setDescription(rs.getString(3));

            }
            rs.close();
            call.close();
            return dto;
        } catch (SQLException ex) {
            return null;
        } finally {
            DatabaseUtil.closeConnection(con);
        }

    }

    public ArrayList<LocationDTO> findByName(String name) {
        ArrayList<LocationDTO> list = null;
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Call_V_Location_FindName(?)}");
            call.setString(1, name);

            rs = call.executeQuery();
            list = new ArrayList<LocationDTO>();
            while (rs.next()) {
                dto = new LocationDTO();
                dto.setLocationId(rs.getInt(1));
                dto.setLocationName(rs.getString(2));
                dto.setDescription(rs.getString(3));

                list.add(dto);
            }
            rs.close();
            call.close();
            return list;
        } catch (SQLException ex) {

            return null;
        }

    }

    public ArrayList<LocationDTO> findByDTO(LocationDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean create(LocationDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            PreparedStatement pst = con.prepareStatement("INSERT INTO Location(LocationName,Description) VALUES(?,?)");
            pst.setString(1, dto.getLocationName());
            pst.setString(2, dto.getDescription());
            pst.executeUpdate();
            pst.close();
            return true;
        } catch (SQLException ex) {
            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean update(LocationDTO dto) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Update_Location(?,?,?)}");
            call.setInt(1, dto.getLocationId());
            call.setString(2, dto.getLocationName());
            call.setString(3, dto.getDescription());
            call.executeUpdate();
            call.close();
            return true;
        } catch (SQLException ex) {
            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
            }
        }
    }

    public boolean delete(int id) {
        try {
            con = DatabaseUtil.getConnection();
            call = con.prepareCall("{call sp_Delete_Location(?)}");
            call.setInt(1, id);
            call.executeUpdate();
            call.close();
            return true;
        } catch (SQLException ex) {
            return false;
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    DatabaseUtil.closeConnection(con);
                }
            } catch (SQLException ex) {
                Logger.getLogger(HotelDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public boolean delete(LocationDTO dto) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ArrayList<SchedulesLocationDTO> showLocationTourName(int id) {
        try {
            con = DatabaseUtil.getConnection();
            ArrayList<SchedulesLocationDTO> listLoactionName = new ArrayList<SchedulesLocationDTO>();
            call = con.prepareCall("SELECT * FROM V_Location WHERE TourId = ?");
            call.setInt(1, id);
            rs = call.executeQuery();
            while (rs.next()) {

                SchedulesLocationDTO dtoL = new SchedulesLocationDTO();
                dtoL.setTourId(rs.getInt(1));
                dtoL.setLocationName(rs.getString(2));
                dtoL.setDescription(rs.getString(3));
                dtoL.setLocationId(rs.getInt(4));
                dtoL.setSchedulesId(rs.getInt(5));
                listLoactionName.add(dtoL);


            }

            return listLoactionName;
        } catch (SQLException ex) {

            return null;
        }
    }

    public ArrayList<LocationDTO> showLocationTourNameN(int id) {
        return null;

    }
}
